package com.example.petback.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.petback.mapper.ItemMapper;
import com.example.petback.model.ItemModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class ItemService {
    @Autowired
    private ItemMapper itemMapper;

    public List<ItemModel> getItemByCategory(String category) {
        QueryWrapper<ItemModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category", category);
        queryWrapper.eq("status", 1);
        return itemMapper.selectList(queryWrapper);
    }

    public ItemModel getItemById(String id) {
        return itemMapper.selectById(id);
    }

    public void setPictureUrl(String url, String id) {
        ItemModel itemModel = itemMapper.selectById(id);
        itemModel.setPicture(url);
        UpdateWrapper<ItemModel> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("picture", url);
        updateWrapper.eq("itemid", id);
        itemMapper.update(itemModel, updateWrapper);
    }

    public boolean updateInfoById(String id, BigDecimal listprice, BigDecimal unicost, Integer inventory, String attr) {
        ItemModel itemModel = itemMapper.selectById(id);
        UpdateWrapper<ItemModel> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("itemid", id);
        updateWrapper.set("listprice", listprice);
        updateWrapper.set("unicost", unicost);
        updateWrapper.set("inventory", inventory);
        updateWrapper.set("attr", attr);
        itemMapper.update(itemModel, updateWrapper);
        return true;
    }

   public boolean insertItem(ItemModel itemModel){
       int insert = itemMapper.insert(itemModel);
       return true;
   }

   public void changeToNo(String id){
       ItemModel itemModel = itemMapper.selectById(id);
       UpdateWrapper<ItemModel> updateWrapper = new UpdateWrapper<>();
       updateWrapper.set("status", 0);
       updateWrapper.eq("itemid",id);
       itemMapper.update(itemModel,updateWrapper);
   }

    public void changeToYes(String id){
        ItemModel itemModel = itemMapper.selectById(id);
        UpdateWrapper<ItemModel> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status", 1);
        updateWrapper.eq("itemid",id);
        itemMapper.update(itemModel,updateWrapper);
    }

    public void updateItem(String id,BigDecimal listprice,BigDecimal unicost,Integer inventory,String attr,String picture){
        ItemModel itemModel = itemMapper.selectById(id);
        UpdateWrapper<ItemModel> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("itemid",id);
        updateWrapper.set("listprice",listprice);
        updateWrapper.set("unicost",unicost);
        updateWrapper.set("inventory",inventory);
        updateWrapper.set("picture",picture);
        itemMapper.update(itemModel,updateWrapper);
    }

    public String getPicture(String itemid){
        ItemModel itemModel = itemMapper.selectById(itemid);
        return itemModel.getPicture();
    }
}
